/**
 *  Java Api For Ecco Generated Applications 
 *  
 *  Copyright (C) 2000  by PDTec GmbH 
 *
 * @author  Michael Weirich
 */

package pdtec.ecco.inst;
 
import pdtec.ecco.meta.Application;
import pdtec.ecco.meta.EccoException;
import pdtec.ecco.meta.Schema;

/**
 * Like Application,the class InstApplication is a proxy for your ECCO generated application.
 * It's the key class to access meta and instance data, because it dynamically loads the ECCO
 * library when passing the library name to its constructor. After loading the   
 * library the native methods of this API are ready for usage.
 * @see  pdtec.ecco.meta.Application
 */
public class InstApplication extends Application {
    
    /** This constant represents the initial EXPRESS aggregate value ("[]"). */
    public AggregateValue    EmptyAggregate = null;

    /** This constant represents the indeterminate EXPRESS value ("?"). */
    public Instance Indeterminate = null;

    /** Output format constant for method writeFile. */
    public final int P21_CC1 = 1;
    /** Output format constant for method writeFile. */
    public final int P21_CC2 = 2;
    /** Output format constant for method writeFile. */
    public final int P28_ETEB = 4;
    /** Output format constant for method writeFile. */
    public final int P28_OSEB = 8;

    /**
     * The constructor is called with the name of the ECCO application to be loaded for instance access. 
     * @param       libName the name of the ecco dll to be loaded
     * @exception pdtec.ecco.meta.EccoException in case an error occurs
     * @see pdtec.ecco.meta.Application
     */
    public InstApplication( String libName ) throws EccoException {
	
	    load( libName, true );
	    setEncodingUTF8();
	    setConstants();
	
    } /* InstApplication */
    
    /** This functions initializes the EmptyAggregate and the Indeterminate members.
     * @see #EmptyAggregate
     * @see #Indeterminate
     * @exception pdtec.ecco.meta.EccoException in case an error occurs
     */
    public native synchronized void setConstants() throws EccoException; 
    
    /** This function sets internal string encoding to UTF8. 
     * @exception pdtec.ecco.meta.EccoException in case an error occurs
     */
    public native synchronized void setEncodingUTF8() throws EccoException;

    /** Calls Algorithm in the underlying ECCO application.
     * @exception pdtec.ecco.meta.EccoException in case an error occurs
     */
    public native synchronized Value callAlgorithm(String schema, String name, Value[] params) throws EccoException;

    /** Calls event in the underlying ECCO application.
     * @exception pdtec.ecco.meta.EccoException in case an error occurs
     */
    public native synchronized void callEvent(String schema, String name, Value[] params) throws EccoException;
 
    /** Write data to SPF file.
     * @param  jfileName (input)
     * @param  format (input), there are three formats supported P21_CC1,P21_CC2 and P28_ETEB
     * @param  schemas : null || schema string list (optional output)
     * @param  headerInfo : null || headerInfo (optional output)
     * @exception pdtec.ecco.meta.EccoException in case an error occurs
    */
    public native synchronized void writeFile( String fileName, 
					       int format,
					       String[] schemas,
					       HeaderInfo headerInfo ) throws EccoException;
    
    /** Write data to SPF file.
     * @param  jfileName (input)
     * @param  format (input)
     * @param  schemas : null || schema object list (optional output)
     * @param  headerInfo : null || headerInfo (optional output)
     * @exception pdtec.ecco.meta.EccoException in case an error occurs
    */
    public void writeFile( String fileName, 
			   int format,
			   Schema[] schemas,
			   HeaderInfo headerInfo ) throws EccoException {
	String[] schemaList = null;  // list of schema names 
	int i;                       // loop counter         
	
	// generate list of schema names
	if( schemas != null) {
	    schemaList = new String[schemas.length];
	    
	    // for each schema copy its name to the string array
	    for( i = 0; i < schemas.length; i++) {
		schemaList[i] = schemas[i].getName();
	    } // for
	    
	    // now call write file method
	    writeFile( fileName,format,schemaList,headerInfo);
	    
	    // otherwise write with default values
	} else {
	    schemaList = null;
	    writeFile( fileName,format,schemaList,headerInfo);
	} // if 
	
    } // writeFile
    
    /** Read data from SPF file.
     * @param jfileName (input)
     * @param format (input)
     * @param jheaderOnly (input)
     * @param headerInfo : null || headerInfo (optional output)
     * @exception pdtec.ecco.meta.EccoException in case an error occurs
    */
    public native synchronized void readFile( String fileName,int format,boolean headerOnly, HeaderInfo info) throws EccoException;
    
    /** Finalizes the application.
     * @see pdtec.ecco.meta.Application#finalize
     */
    
    
    protected void finalize() {
	super.finalize();
	
    } // finalize
    
} // InstApplication





